//
//  SearchAddressViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/28.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "SearchAddressViewController.h"
#import "NearbyTableViewCell.h"//附近cell
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>

@interface SearchAddressViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIScrollViewDelegate, BMKGeoCodeSearchDelegate>

@property (weak, nonatomic) IBOutlet UIView *searchbgView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UITableView *searchTableView;

@end

@implementation SearchAddressViewController
{
    
    BMKGeoCodeSearch *_searcher;
}

 static NSString *const NearbyTableViewCellID = @"NearbyTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _searchTextField.text = _searchString;
    _searchbgView.layer.borderWidth = 1;
    _searchbgView.layer.borderColor = RGBCOLOR(226, 235, 241).CGColor;
    [_searchTableView registerNib:[UINib nibWithNibName:@"NearbyTableViewCell" bundle:nil] forCellReuseIdentifier:NearbyTableViewCellID];
    
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    [self getCoordinateWithCity:nil AndAddress:_searchString];
}

#pragma mark === 地理编码检索 ===
- (void)getCoordinateWithCity:(NSString *)city  AndAddress:(NSString *)address{
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.city = @"杭州市";
    geoCodeSearchOption.address = address;
    
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];
    geoCodeSearchOption = nil;
    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
        [SVProgressHUD dismiss];
    }
}
#pragma mark === 地理检索回调 ===
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        DLog(@"%@    %f, %f", result.address, result.location.longitude, result.location.latitude);
        
    }
    else {
        //        NSLog(@"抱歉，未找到结果, 请手动定位");
    }
    [SVProgressHUD dismiss];
    
}







#pragma mark === TableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NearbyTableViewCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


#pragma mark === UITextFieldDelegate ===
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    DLog(@"%@", textField.text);
    [self getCoordinateWithCity:nil AndAddress:textField.text];
    [_searchTextField resignFirstResponder];
    return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchTextField resignFirstResponder];
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
